package com.cj.controller;

import java.util.List;

import com.cj.model.Department;
import com.cj.model.Post;
import com.cj.utils.StrUtil;
import com.jfinal.core.Controller;

public class DepartmentController extends Controller {
	
	public void index() {
		List<Department> departments = Department.me.getAllDepartments();
		setAttr("departments", departments);
		render("departmentInfo.jsp");
	}
	
	public void edit() {
		getModel(Department.class).update();
		redirect("/department");
	}
	
	public void add() {
		Department department = getModel(Department.class);
		Integer maxId = Department.me.getMaxID();
		String deptNo = StrUtil.getNo("D", "000", maxId + 1);
		department.remove("deptId");
		department.set("deptNo", deptNo).save();
		redirect("/department");
	}
	
	public void delete() {
		Integer deptId = getParaToInt("deptId");
		List<Post> posts = Post.me.getPostsByDept(deptId);
		if(posts == null || posts.isEmpty()) {
			Department.me.deleteById(getParaToInt("deptId"));
			renderJson("flag", true);
		} else {
			renderJson("flag", false);
		}
	}
}
